#include "Emitter.h"
#include "Particle.h"
#include "../../SGD Wrappers/CSGD_TextureManager.h"
#include <sstream>

Emitter::Emitter(int NumOfParticles, bool Looping, float Life, const char* ImageID,
				 float PosX, float PosY, int Shape, float SRate, float SRange, int SA,
				 int SR, int SG, int SB, int EA, int ER, int EG, int EB, float StartVelx,
				 float StartVely, float EndVelx, float EndVely, float SRot, float ERot,
				 float SScale, float EScale)
{
	m_nNumOfParticles = NumOfParticles;
	m_bIsLooping = Looping;
	m_fCurrLife = Life;
	m_fPosX = PosX;
	m_fPosY = PosY;
	m_nSpawnShape = Shape;
	m_fSpawnRate = SRate;
	m_fSpawnRange = SRange;
	m_nStartAlpha = SA;
	m_nEndAlpha = EA;
	m_nStartR = SR;
	m_nEndR = ER;
	m_nStartG = SG;
	m_nEndG = EG;
	m_nStartB = SB;
	m_nEndB = EB;
	m_fStartVelX = StartVelx;
	m_fEndVelX = EndVelx;
	m_fStartVelY = StartVely;
	m_fEndVelY = EndVely;
	m_fStartRotation = SRot;
	m_fEndRotation = ERot;
	m_fStartScale = SScale;
	m_fEndScale = EScale;


	m_fTimer = 0.0f;

	if(ImageID)
	{
		wostringstream Stuff;
		Stuff << ImageID;
		NameOfImageID = ImageID;
		m_nPartImageID = CSGD_TextureManager::GetInstance()->LoadTexture(Stuff.str().c_str());

		m_vTotalParticles.reserve(m_nNumOfParticles);
	}
}


Emitter::~Emitter(void)
{

	for(vector<Particle*>::iterator Iter = m_vTotalParticles.begin(); Iter != m_vTotalParticles.end(); ++Iter)
	{
		//AllEmitters.erase(Iter);
		delete *Iter;
	}

}

void Emitter::Update(float fElapsedTime)
{
	m_fTimer += fElapsedTime;

	for (unsigned int ParticleIndex = 0; ParticleIndex  < (unsigned int)m_nNumOfParticles; ParticleIndex ++)
	{
		if((unsigned int)m_nNumOfParticles > m_vTotalParticles.size() && m_fTimer >= m_fSpawnRate)
		{
			m_fTimer = 0.0f;
			Particle * newPart = new Particle(this);
			m_vTotalParticles.push_back(newPart);
		}
		else if(ParticleIndex < m_vTotalParticles.size() && m_vTotalParticles[ParticleIndex]->GetLife() > 0.0f)
		{
			m_vTotalParticles[ParticleIndex]->Update(fElapsedTime);
		}
		else if (ParticleIndex < m_vTotalParticles.size() && m_bIsLooping)
		{
			m_vTotalParticles[ParticleIndex]->Renew();
		}
		/*else 
			RemoveParticle(ParticleIndex);*/
		
	}
}

void Emitter::Render()
{
	for (unsigned int ParticleIndex = 0; ParticleIndex  < m_vTotalParticles.size(); ParticleIndex ++)
	{
		//Particle Resolution
		if(m_vTotalParticles[ParticleIndex]->GetLife() > 0.0f)
		{
			RECT Draw = {0,0, 64, 64};
			CSGD_TextureManager::GetInstance()->Draw(m_nPartImageID, (int)m_vTotalParticles[ParticleIndex]->GetPosX(),  (int)m_vTotalParticles[ParticleIndex]->GetPosY(),
				m_vTotalParticles[ParticleIndex]->GetScale(),  m_vTotalParticles[ParticleIndex]->GetScale(), &Draw,  0,
				0,  m_vTotalParticles[ParticleIndex]->GetRotation(), 
				D3DCOLOR_ARGB( m_vTotalParticles[ParticleIndex]->GetColorA(),  m_vTotalParticles[ParticleIndex]->GetColorR(),  m_vTotalParticles[ParticleIndex]->GetColorG() ,  m_vTotalParticles[ParticleIndex]->GetColorB()));
		}
	}
}

void Emitter::LoadNewParticles(int size)
{
	for (int ParticleIndex = 0;  ParticleIndex < size;  ParticleIndex++)
	{
		Particle * newPart = new Particle(this);
		m_vTotalParticles.push_back(newPart);
	}
}

void Emitter::RemoveParticle(int INDEX)
{
	int index = 0;
	delete m_vTotalParticles[INDEX];

	for(vector<Particle*>::iterator Iter = m_vTotalParticles.begin(); Iter != m_vTotalParticles.end(); ++Iter)
	{
		if(index == INDEX)
		{
			m_vTotalParticles.erase(Iter);
			break;
		}
		else 
			index++;
	}
}